package szzy

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/szzy"
    szzyReq "github.com/flipped-aurora/gin-vue-admin/server/model/szzy/request"
)

type StudentScoresService struct {}
// CreateStudentScores 创建成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService) CreateStudentScores(CJ *szzy.StudentScores) (err error) {
	err = global.GVA_DB.Create(CJ).Error
	return err
}

// DeleteStudentScores 删除成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService)DeleteStudentScores(ID string) (err error) {
	err = global.GVA_DB.Delete(&szzy.StudentScores{},"id = ?",ID).Error
	return err
}

// DeleteStudentScoresByIds 批量删除成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService)DeleteStudentScoresByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]szzy.StudentScores{},"id in ?",IDs).Error
	return err
}

// UpdateStudentScores 更新成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService)UpdateStudentScores(CJ szzy.StudentScores) (err error) {
	err = global.GVA_DB.Model(&szzy.StudentScores{}).Where("id = ?",CJ.ID).Updates(&CJ).Error
	return err
}

// GetStudentScores 根据ID获取成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService)GetStudentScores(ID string) (CJ szzy.StudentScores, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&CJ).Error
	return
}

// GetStudentScoresInfoList 分页获取成绩模块记录
// Author [piexlmax](https://github.com/piexlmax)
func (CJService *StudentScoresService)GetStudentScoresInfoList(info szzyReq.StudentScoresSearch) (list []szzy.StudentScores, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&szzy.StudentScores{})
    var CJs []szzy.StudentScores
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Uid != nil {
        db = db.Where("uid = ?",info.Uid)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&CJs).Error
	return  CJs, total, err
}
func (CJService *StudentScoresService)GetStudentScoresPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
